<?php

declare(strict_types=1);

namespace App\Helper;
use Hyperf\Context\Context;
use Psr\Http\Message\ServerRequestInterface;
use Hyperf\DbConnection\Db;
class Common
{
    /**
     * 获取客户端IP
     *
     * @return void
     */
    public static function getUserIp()
    {
        $request = Context::get(ServerRequestInterface::class);
        $res = $request->getServerParams();
        if(isset($res['http_client_ip'])){
            return $res['http_client_ip'];
        }elseif(isset($res['http_x_real_ip'])){
            return $res['http_x_real_ip'];
        }elseif(isset($res['http_x_forwarded_for'])){
            //部分CDN会获取多层代理IP，所以转成数组取第一个值
            $arr = explode(',',$res['http_x_forwarded_for']);
            return $arr[0];
        }else{
            return $res['remote_addr'];
        }
    }
    /**
     * 获取系统配置参数
     * [getSystemConf description]
     * @param  [type] $key [description]
     * @return [type]      [description]
     */
    public static function getSystemConf($key)
    {
        $keys = [
            'color'=>['id','name','color'],
            'education'=>['id','name'],
            'nurse_level'=>['id','name'],
            'positional'=>['id','name'],
            'tbed'=>['id','name'],
            'tmod'=>['id','name'],
            'tunit'=>['id','name'],
        ];
        if(!array_key_exists($key, $keys)){
            return [];
        }
        $table = "conf_".$key;
        $datas = Db::table($table)
            ->select($keys[$key])
            ->where(['is_del'=>0,'status'=>1])
            ->orderBy('sort', 'desc')
            ->get()
            ->toArray();
        return $datas;
    }
    /**
     * 获取省
     * [getProvince description]
     * @return [type] [description]
     */
    public static function getProvince()
    {
        $datas = Db::table("conf_provinces")
            ->select(['id','name'])
            ->where([['class', '=', 1]])
            ->orderBy('initial', 'asc')
            ->get()
            ->toArray();
        return $datas;
    }
    /**
     * 获取市
     * [getCity description]
     * @param  [type] $parent_id [description]
     * @return [type]              [description]
     */
    public static function getCity($parent_id)
    {
        if(empty($parent_id)){
            return [];
        }
        $parent_code = Db::table("conf_provinces")
            ->where([['id', '=', $parent_id]])
            ->value('code');
        $datas = Db::table("conf_provinces")
            ->select(['id','name'])
            ->where([['class', '=', 2],['parent_code', '=', $parent_code]])
            ->orderBy('initial', 'asc')
            ->get()
            ->toArray();
        return $datas;
    }
    /**
     * 获取全部市
     * [getAllCity description]
     * @return [type] [description]
     */
    public static function getAllCity()
    {
        $hot = Db::table("conf_provinces_hot")
            ->select(['id','name','initial'])
            ->where([['class', '=', 2]])
            ->orderBy('sort', 'desc')
            ->get()
            ->toArray();
        $datas = Db::table("conf_provinces")
            ->select(['id','name','initial'])
            ->where([['class', '=', 2],['name', '<>', "直辖县"]])
            ->orderBy('initial', 'asc')
            ->get()
            ->toArray();
        $return_data = [
            'hot'=>$hot,
            'list'=>$datas
        ];
        return $return_data;
    }
    /**
     * 根据ID获取省名称
     * [getProvinceById description]
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public static function getProvinceById($id)
    {
        $name = Db::table("conf_provinces")
            ->where(["id"=>$id,'class'=>1])
            ->value("name");
        return $name;
    }
    /**
     * 根据ID获取市名称
     * [getCityById description]
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public static function getCityById($id)
    {
        $name = Db::table("conf_provinces")
            ->where(["id"=>$id,'class'=>2])
            ->value("name");
        return $name;
    }
}
